/**   
 * 项目名：五位一体化工安全信息平台
 * 日期：    2019-12-06 09:51:29  
 * Copyright (c) 2015- joysuch-版权所有     
 */
package com.joysuch.wwyt.bp.service.impl;

import javax.transaction.Transactional;

import com.joysuch.wwyt.bp.entity.BpRegulationDocument;
import com.joysuch.wwyt.bp.enums.RegulationDocumentTypes;
import com.joysuch.wwyt.bp.repository.BpRegulationDocumentDao;
import com.joysuch.wwyt.core.service.BizCodeGeneratorFacade;
import com.joysuch.wwyt.bp.entity.BpRegulationDocumentArchive;
import com.joysuch.wwyt.bp.repository.BpRegulationDocumentArchiveDao;
import com.joysuch.wwyt.bp.service.BpRegulationDocumentArchiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Transactional
public class BpRegulationDocumentArchiveServiceImpl implements BpRegulationDocumentArchiveService {

    @Autowired
    private BpRegulationDocumentArchiveDao dao;
    @Autowired
    private BizCodeGeneratorFacade bizCodeGeneratorFacade;

    @Autowired
    private BpRegulationDocumentDao bpRegulationDocumentDao;

    @Override
    public void add(BpRegulationDocumentArchive entity) throws Exception {
        dao.save(entity);
    }

    @Override
    public void update(BpRegulationDocumentArchive entity) throws Exception {
        BpRegulationDocumentArchive content = dao.findById(entity.getId()).get();
        if (content != null) {
            //TODO set nessessary properties
            dao.save(content);
        }
    }

    @Override
    public Page<BpRegulationDocumentArchive> findByPage(Pageable page) {
        return dao.findAll(page);
    }

    @Override
    public void delete(Long id) {
        dao.deleteById(id);
    }
    
    @Override
    public BpRegulationDocumentArchive findById(Long id) {
        return dao.findById(id).get();
    }

    @Override
    public void archive(Long id) {
        // 获取待归档（重新编制）的文档对象（原文档）
        BpRegulationDocument bpRegulationDocument = bpRegulationDocumentDao.findById(id).get();
        // 将原文档的信息添加到归档表中
        BpRegulationDocumentArchive regDocArcAdd = new BpRegulationDocumentArchive();
        regDocArcAdd.setDocumentId(bpRegulationDocument.getId());
        regDocArcAdd.setName(bpRegulationDocument.getName());
        regDocArcAdd.setDocumentTypeId(bpRegulationDocument.getDocumentTypeId());
        regDocArcAdd.setMakeUserId(bpRegulationDocument.getMakeUserId());
        regDocArcAdd.setState(bpRegulationDocument.getState());
        regDocArcAdd.setContent(bpRegulationDocument.getContent());
        regDocArcAdd.setAttachmentId(bpRegulationDocument.getAttachmentId());
        regDocArcAdd.setMakeTime(bpRegulationDocument.getMakeTime());
        regDocArcAdd.setRemark(bpRegulationDocument.getRemark());
        regDocArcAdd.setAuditUserId(bpRegulationDocument.getAuditUserId());
        regDocArcAdd.setAuditTime(bpRegulationDocument.getAuditTime());
        regDocArcAdd.setAuditRemark(bpRegulationDocument.getAuditRemark());
        regDocArcAdd.setArchiveTime(new Date());
        dao.save(regDocArcAdd);
        // 重置原文档的状态
        bpRegulationDocument.setState(RegulationDocumentTypes.TO_BE_AUDITED.getId());
        bpRegulationDocument.setAuditUserId(null);
        bpRegulationDocument.setAuditTime(null);
        bpRegulationDocument.setAuditRemark(null);
        bpRegulationDocumentDao.save(bpRegulationDocument);
    }
}
